'''
处理温度相关数据
'''
import re

from excutor.excutor import Excutor

class Temperature(Excutor):

    def __init__(self, data):
        super().__init__(data)
        self.temperature = '温度'
        
    @staticmethod
    def Existence(data,temperature = '温度'):
        return data[data['new_key'].str.contains(temperature)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexTem = self.data[self.data['new_key'].str.contains(self.temperature)].index
        for index in indexTem:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data
    
    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        if oneRowData['new_key'] == '工作温度':
            return self._working_T_range(oneRowData)

        if oneRowData['new_key'] == '温度系数' \
            or oneRowData['new_key'] == '温度分辨率':
            return oneRowData['old_value']
        
        if oneRowData['new_key'] == '焊接温度':
            return oneRowData['old_value']

        if oneRowData['new_key'] == '断电温度' \
            or oneRowData['new_key'] == '通电温度':
            return self._pw_on_T_and_pw_off_T(oneRowData)
        
        return self._other_T(oneRowData)

    

    # 工作温度
    def _working_T_range(self,oneRowData):
        old_value = oneRowData['old_value']
        numbers = re.findall('[+-]?[0-9]+',old_value)
        numbers = self.deduplication_and_sort(numbers)
        if len(numbers) == 1:
            return numbers[0] + '℃'
        if len(numbers) > 1:
            return numbers[0] + ' to ' + numbers[-1] + '℃'
        return old_value
    
    # 通电温度和断电温度
    def _pw_on_T_and_pw_off_T(self,oneRowData):
        old_value = oneRowData['old_value']
        temper = re.findall(r'.*[0-9]+\.?[0-9]*°C',old_value)
        if temper:
            return temper[0]
        return old_value
    
    # 其它温度
    def _other_T(self,oneRowData):
        return self._working_T_range(oneRowData)
        
    

    # 对数字去重并排序
    def deduplication_and_sort(self,numbers):
        numbers = set(numbers)
        numbersInt = sorted([int(number) for number in numbers])
        return [str(num) for num in numbersInt]